package controller;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import pojo.Book;
import pojo.Comment;
import pojo.Status;
import pojo.Thongso;
import pojo.Topic;
import pojo.User;

import java.util.*;

import dao.BookDAO;
import dao.CommentDAO;
import dao.StatusDAO;
import dao.ThongsoDAO;
import dao.TopicDAO;
import dao.UserDAO;
 
@Controller
public class CommentController {
	   
		static private int slcmtt=-1;
		static private int page=-1;
		static private int bookid=-1;
		static private String userid="";
		
    @RequestMapping(value= "/Comment.do" , method = RequestMethod.POST)
    public String getview(HttpServletRequest request,
    		@RequestParam(value="bookid", required = false) String bookid,
			@RequestParam(value ="userid", required = false) String userid,
			@RequestParam(value ="noidungComment", required = false) String noidung,
			Model model
    		) {
    	
    	BookDAO bd=new BookDAO();
    	Book b= bd.getBookByID(Integer.parseInt(bookid));
    	UserDAO ud= new UserDAO();
    	User u= ud.getUser(userid);
    	if(u==null)
    	{
    		model.addAttribute("output","ID sách không đúng");
    		model.addAttribute("link","home");
    		return ("template/ketqua");
    	}
    	if(u.getStatus().getIdstatus()==5)
    	{
    		model.addAttribute("output","Tài khoản đang bị khóa, không thể gửi comment. Vui lòng liên hệ Admin");
    	  	model.addAttribute("link","ChiTietSach.do?bookid="+bookid);
    		return ("template/ketqua");
    	}
    	CommentDAO cd= new CommentDAO();
    	boolean flag= cd.insert(b, u, noidung);
    	if(flag==false)
    	{
	    	model.addAttribute("output","Thêm bình luận không thành công");
	    	model.addAttribute("link","ChiTietSach.do?bookid="+bookid);
	     	return ("template/ketqua");
    	}
    	else
    	{
    		model.addAttribute("output","Thêm bình luận thành công");
	    	model.addAttribute("link","ChiTietSach.do?bookid="+bookid);
	    	String redi="redirect:ChiTietSach.do?bookid="+bookid;
	    	return (redi);
    	}
   
    }

	@RequestMapping (value = "LietKeCommentChuaDuyet.do")
	public String lietkeCommentChuaDuyet(
			@RequestParam(value = "trang", required = false) String trang,
			@RequestParam(value = "slcmtt", required = false) String soLuongCmTrenTrang,
			@RequestParam(value = "save", required = false) String save,
			HttpServletRequest request)
	{
		HttpSession session = request.getSession();
    	User us = (User) session
				.getAttribute("user");
    	if(us==null)       return ("page/FrontEnd/DangNhap");
    	if(us.getIsAdmin()==0) return ("page/FrontEnd/DangNhap");
		if(save.equals("false"))
		{
			slcmtt=-1;
			page=-1;
		}
		if(soLuongCmTrenTrang != null) slcmtt= Integer.parseInt(soLuongCmTrenTrang);
		if(trang!=null) page=Integer.parseInt(trang);
		CommentDAO dao= new CommentDAO();
		long tongtrang=dao.tinhsotrangChuaDuyet(slcmtt);
		List<Comment> lcm= dao.layCommentChuaduyet(page,slcmtt);
		
		
		request.setAttribute("bookcm", null);
		request.setAttribute("tongSoTrang", tongtrang);
		request.setAttribute("slcmtt", slcmtt);
		request.setAttribute("Servlet", "LietKeCommentChuaDuyet.do?save=true");
		request.setAttribute("trangHienTai", page);
		String st2= request.getRequestURL().toString();
    	String oldUrl=st2+"?&trang="+Integer.toString(page)+"&slusertt="+Integer.toString(slcmtt)+"&save=true";
    	session.setAttribute("oldlink", oldUrl);
		request.setAttribute("lcm", lcm);		
		request.setAttribute("title", "Liệt kê Comment chưa duyệt");
		request.setAttribute("user", null);
		return "page/BackEnd/LietKeComment";
	}	
   
	@RequestMapping (value = "CapNhatTrangThaiComment.do")
	public String capnhatChuaDuyet(
			@RequestParam(value = "status", required = false) String status,
			@RequestParam(value = "cmid", required = false) String id,
			HttpServletRequest request)
	{
		HttpSession session = request.getSession();
    	User us = (User) session
				.getAttribute("user");
    	if(us==null)       return ("page/FrontEnd/DangNhap");
    	if(us.getIsAdmin()==0) return ("page/FrontEnd/DangNhap");
		String link=(String)session.getAttribute("oldlink");
		String url="admin";
		if(link!=null) url=link;
		CommentDAO dao= new CommentDAO();
		Comment cm= dao.getCommentByID(Integer.parseInt(id));
		StatusDAO stdao= new StatusDAO();
		Status st= stdao.getStatusByID(Integer.parseInt(status));
		
		if(st==null ||cm==null)
		{
			request.setAttribute("output","Cập nhật thất bại");	    	
	    	request.setAttribute("link",url);
			return "template/ketqua";
		}
		cm.setStatus(st);
		boolean flag= dao.capnhatTrangThaiComment(cm);
		if(flag==true)
		{
			request.setAttribute("output","Cập nhật thành công");
			request.setAttribute("link",url);
		}
		else
		{
			request.setAttribute("output","Cập nhật thất bại");	    	
	    	request.setAttribute("link",url);
		}
		return "template/ketqua";
		
	}
	
	  
		@RequestMapping (value = "XoaComment.do")
		public String XoaComment(
				@RequestParam(value = "cmid", required = false) String id,
				HttpServletRequest request)
		{

			HttpSession session = request.getSession();
	    	User us = (User) session
					.getAttribute("user");
	    	if(us==null)       return ("page/FrontEnd/DangNhap");
	    	if(us.getIsAdmin()==0) return ("page/FrontEnd/DangNhap");
			String link=(String)session.getAttribute("oldlink");
			String url="admin";
			if(link!=null) url=link;
			CommentDAO dao= new CommentDAO();
			Comment cm= dao.getCommentByID(Integer.parseInt(id));

			
			if(cm==null)
			{
				request.setAttribute("output","Xóa thất bại");	    	
		    	request.setAttribute("link",url);
				return "template/ketqua";
			}
			boolean flag= dao.XoaComment(cm);
			if(flag==true)
			{
				request.setAttribute("output","Xóa thành công");
				request.setAttribute("link",url);
			}
			else
			{
				request.setAttribute("output","Xóa thất bại");	    	
		    	request.setAttribute("link",url);
			}
			return "template/ketqua";
			
		}
		
		
		@RequestMapping (value = "LietKeCommentTheoSach.do")
		public String lietkeCommentTheoSach(
				@RequestParam(value = "bookid", required = false) String _bookid,
				@RequestParam(value = "trang", required = false) String trang,
				@RequestParam(value = "slcmtt", required = false) String soLuongCmTrenTrang,
				@RequestParam(value = "save", required = false) String save,
				HttpServletRequest request)
		{
			HttpSession session = request.getSession();
	    	User us = (User) session
					.getAttribute("user");
	    	if(us==null)       return ("page/FrontEnd/DangNhap");
	    	if(us.getIsAdmin()==0) return ("page/FrontEnd/DangNhap");
			if(save.equals("false"))
			{
				slcmtt=-1;
				page=-1;
				bookid=-1;
			}
		
			if(soLuongCmTrenTrang != null) slcmtt= Integer.parseInt(soLuongCmTrenTrang);
			if(trang!=null) page=Integer.parseInt(trang);
			if(_bookid!=null) bookid=Integer.parseInt(_bookid);
			BookDAO bd= new BookDAO();
			Book b= bd.getBookByID(bookid);
			if(b==null)
			{
				request.setAttribute("output","Sach không tồn tại");	    	
		    	request.setAttribute("link","admin");
				return "template/ketqua";
			}
			
			CommentDAO dao= new CommentDAO();
			long tongtrang=dao.tinhsotrangTheoSach(b, slcmtt);
			List<Comment> lcm= dao.layCommentTheoSach(b,page,slcmtt);
			
			request.setAttribute("usercm", null);
			request.setAttribute("bookcm", b);
			request.setAttribute("tongSoTrang", tongtrang);
			request.setAttribute("slcmtt", slcmtt);
			request.setAttribute("Servlet", "LietKeCommentTheoSach.do?save=true");
			request.setAttribute("trangHienTai", page);
			String st2= request.getRequestURL().toString();
	    	String oldUrl=st2+"?bookid="+Integer.toString(bookid)+"&trang="+Integer.toString(page)+"&slcmtt="+Integer.toString(slcmtt)+"&save=true";
	    	session.setAttribute("oldlink", oldUrl);
			request.setAttribute("lcm", lcm);		
			request.setAttribute("title", "Liệt kê Comment theo sách");
			return "page/BackEnd/LietKeComment";
		}	
		
		
		@RequestMapping (value = "LietKeCommentTheoUser.do")
		public String lietkeCommentTheoUser(
				@RequestParam(value = "userid", required = false) String _userid,
				@RequestParam(value = "trang", required = false) String trang,
				@RequestParam(value = "slcmtt", required = false) String soLuongCmTrenTrang,
				@RequestParam(value = "save", required = false) String save,
				HttpServletRequest request)
		{
			HttpSession session = request.getSession();
	    	User us = (User) session
					.getAttribute("user");
	    	if(us==null)       return ("page/FrontEnd/DangNhap");
	    	if(us.getIsAdmin()==0) return ("page/FrontEnd/DangNhap");
			if(save.equals("false"))
			{
				slcmtt=-1;
				page=-1;
				userid="";
			}
		
			if(soLuongCmTrenTrang != null) slcmtt= Integer.parseInt(soLuongCmTrenTrang);
			if(trang!=null) page=Integer.parseInt(trang);
			if(_userid!=null) userid=_userid;
			UserDAO bd= new UserDAO();
			User b= bd.getUser(userid);
			if(b==null)
			{
				request.setAttribute("output","Tài khoản không tồn tại");	    	
		    	request.setAttribute("link","admin");
				return "template/ketqua";
			}
			
			CommentDAO dao= new CommentDAO();
			long tongtrang=dao.tinhsotrangTheoUser(b, slcmtt);
			List<Comment> lcm= dao.layCommentTheoUser(b,page,slcmtt);
			
			request.setAttribute("bookcm", null);
			request.setAttribute("usercm", b);
			request.setAttribute("tongSoTrang", tongtrang);
			request.setAttribute("slcmtt", slcmtt);
			request.setAttribute("Servlet", "LietKeCommentTheoUser.do?save=true");
			request.setAttribute("trangHienTai", page);
			String st2= request.getRequestURL().toString();
	    	String oldUrl=st2+"?bookid="+Integer.toString(bookid)+"&trang="+Integer.toString(page)+"&slcmtt="+Integer.toString(slcmtt)+"&save=true";
	    	session.setAttribute("oldlink", oldUrl);
			request.setAttribute("lcm", lcm);		
			request.setAttribute("title", "Liệt kê Comment theo tài khoản");
			return "page/BackEnd/LietKeComment";
		}	
}